package com.example.dreamlist2;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import java.lang.reflect.Field;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class Utils {

    //传送数据
    public static void launchForResult(Activity activity1, Class activity2, String parm, String data){
        Intent intent = new Intent(activity1, activity2);
        intent.putExtra(parm, data);
        activity1.startActivity(intent);
    }

    //传送bundle
    //bundle中的键值成对存在
    public static void launch(Activity activity1, Class activity2, Bundle bundle, String parm){
        Intent intent = new Intent(activity1, activity2);
        intent.putExtra(parm, bundle);
        activity1.startActivity(intent);
    }


    //修改用户名
    public static boolean change_uname(String new_uname, String uid){
        final boolean[] res = new boolean[1];
        User newu = new User();
        newu.setUname(new_uname);
        newu.update(uid, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    res[0] = true;
                    Log.i("bmob","更新成功");
                }else{
                    res[0] = false;
                    Log.i("bmob","更新失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
        return res[0];
    }

    //修改密码
    public static boolean change_pswd(String new_pswd, String uid){
        final boolean[] res = new boolean[1];
        User newu = new User();
        newu.setPassword(new_pswd);
        newu.update(uid, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    res[0] = true;
                    Log.i("bmob","更新成功");
                }else{
                    res[0] = false;
                    Log.i("bmob","更新失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
        return res[0];
    }

    //获取图片路径
    public static int getimages(String name){
        Class drawable = R.drawable.class;
        Field field = null;
        try {
            field =drawable.getField(name);
            int images = field.getInt(field.getName());
            return images;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}
